﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using ICSharpCode.SharpZipLib.Zip;
using System.Threading;
using System.Globalization;

namespace WidgetConfigFileGenerator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void UpdateUI()
        {
            Properties.Settings s = Properties.Settings.Default;
            s.UICulture = Thread.CurrentThread.CurrentUICulture.Name;
            s.Save();

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

            resources.ApplyResources(this.label1, "label1");
            resources.ApplyResources(this.txtWidgetName, "txtWidgetName");
            resources.ApplyResources(this.label2, "label2");
            resources.ApplyResources(this.txtWidgetFile, "txtWidgetFile");
            resources.ApplyResources(this.btnBrowseHtm, "btnBrowseHtm");
            resources.ApplyResources(this.label3, "label3");
            resources.ApplyResources(this.txtWidgetIcon, "txtWidgetIcon");
            resources.ApplyResources(this.btnBrowsePng, "btnBrowsePng");
            resources.ApplyResources(this.lbWidgetDescription, "lbWidgetDescription");
            resources.ApplyResources(this.txtWidgetDescription, "txtWidgetDescription");
            resources.ApplyResources(this.btnCreate, "btnCreate");
            resources.ApplyResources(this.treeView1, "treeView1");
            resources.ApplyResources(this.label4, "label4");
            resources.ApplyResources(this.groupBox1, "groupBox1");
            resources.ApplyResources(this.txtAuthorURL, "txtAuthorURL");
            resources.ApplyResources(this.label7, "label7");
            resources.ApplyResources(this.txtAuthorEmail, "txtAuthorEmail");
            resources.ApplyResources(this.label6, "label6");
            resources.ApplyResources(this.txtAuthorName, "txtAuthorName");
            resources.ApplyResources(this.label5, "label5");
            resources.ApplyResources(this.label8, "label8");
            resources.ApplyResources(this.txtWidgetID, "txtWidgetID");
            resources.ApplyResources(this.label9, "label9");
            resources.ApplyResources(this.txtWidgetVersion, "txtWidgetVersion");
            resources.ApplyResources(this.menuStrip1, "menuStrip1");
            resources.ApplyResources(this.mnuLanguage, "mnuLanguage");
            resources.ApplyResources(this.mnuChinese, "mnuChinese");
            resources.ApplyResources(this.mnuEnglish, "mnuEnglish");
            resources.ApplyResources(this, "$this");
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            foreach (TreeNode n in e.Node.Nodes)
            {
                n.Checked = e.Node.Checked;
            }
        }

        private void AddDirectoryFile(string dirName)
        {
            TreeNode node = treeView1.Nodes.Add(dirName);

            DirectoryInfo di = new DirectoryInfo(dirName);
            foreach (FileInfo fi in di.GetFiles())
            {
                TreeNode n = node.Nodes.Add(fi.Name);
                n.ImageIndex = 2;
                n.SelectedImageIndex = 2;
                n.ToolTipText = fi.LastWriteTime.ToString();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ofdHtm.InitialDirectory = Application.StartupPath;
            ofdPng.InitialDirectory = Application.StartupPath;

            string strPath = Application.StartupPath;
            DirectoryInfo di = new DirectoryInfo(strPath);

            foreach (DirectoryInfo d in di.GetDirectories())
            {
                AddDirectoryFile(d.Name);
            }

            foreach (FileInfo fi in di.GetFiles("*.*"))
            {
                TreeNode node=treeView1.Nodes.Add(fi.Name);
                node.ImageIndex = 2;
                node.SelectedImageIndex = 2;
                node.ToolTipText = fi.LastWriteTime.ToString();
            }
        }

        private void btnBrowseHtm_Click(object sender, EventArgs e)
        {
            if (ofdHtm.ShowDialog() == DialogResult.OK)
            {
                txtWidgetFile.Text = Path.GetFileName(ofdHtm.FileName);
            }
        }

        private void btnBrowsePng_Click(object sender, EventArgs e)
        {
            if (ofdPng.ShowDialog() == DialogResult.OK)
            {
                txtWidgetIcon.Text = Path.GetFileName(ofdPng.FileName);
                pbWidgetIcon.ImageLocation=ofdPng.FileName;
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            bool bOkToCreate = true;

            if (txtWidgetName.Text.Length == 0)
            {
                errorProvider1.SetError(txtWidgetName, Resource1.strWidgetName);
                bOkToCreate = false;
            }
            if (txtWidgetFile.Text.Length == 0)
            {
                errorProvider1.SetError(txtWidgetFile, Resource1.strWidgetFile);
                bOkToCreate = false;
            }
            if (txtWidgetIcon.Text.Length == 0)
            {
                errorProvider1.SetError(txtWidgetIcon, Resource1.strWidgetIcon);
                bOkToCreate = false;
            }
            if (txtWidgetDescription.Text.Length == 0)
            {
                errorProvider1.SetError(lbWidgetDescription, Resource1.strWidgetDescription);
                bOkToCreate = false;
            }
            if (txtWidgetID.Text.Length == 0)
            {
                errorProvider1.SetError(txtWidgetID, Resource1.strWidgetID);
                bOkToCreate = false;
            }
            if (txtWidgetVersion.Text.Length == 0)
            {
                errorProvider1.SetError(txtWidgetVersion, Resource1.strWidgetVersion);
            }

            if (bOkToCreate)
            {
                //Create xonfig.xml
                XmlTextWriter xw = new XmlTextWriter("config.xml", Encoding.UTF8);      
                xw.WriteStartDocument();                                            //<?xml version="1.0" encoding="utf-8" ?> 
               
                xw.WriteStartElement("widget");                                     //<widget version="1.0" xmlns="http://www.w3.org/ns/widgets" id="">
                xw.WriteAttributeString("version", txtWidgetVersion.Text);
                xw.WriteAttributeString("xmlns", "http://www.w3.org/ns/widgets");
                xw.WriteAttributeString("id", txtWidgetID.Text);

                xw.WriteStartElement("name");                                       //<name>My First Widget</name> 
                xw.WriteString(txtWidgetName.Text);
                xw.WriteEndElement();

                xw.WriteStartElement("author");                                     //<author href="http://home.live.com/" email="John@test.com">John</author> 
                xw.WriteAttributeString("href", txtAuthorURL.Text);
                xw.WriteAttributeString("email", txtAuthorEmail.Text);
                xw.WriteString(txtAuthorName.Text);
                xw.WriteEndElement();

                xw.WriteStartElement("content");                                    //<content src="FirstWidget.htm" type="text/html" /> 
                xw.WriteAttributeString("src", txtWidgetFile.Text);
                xw.WriteAttributeString("type", "text/html");
                xw.WriteEndElement();

                xw.WriteStartElement("access");                                     //<access network="true" /> 
                xw.WriteAttributeString("network", "true");
                xw.WriteEndElement();

                xw.WriteStartElement("icon");                                       //<icon src="Widget.png" /> 
                xw.WriteAttributeString("src", txtWidgetIcon.Text);
                xw.WriteEndElement();

                xw.WriteStartElement("description");                                //<description>This is my first Widget</description> 
                xw.WriteString(txtWidgetDescription.Text);
                xw.WriteEndElement();

                xw.WriteEndElement();                                               //</widget>

                xw.WriteEndDocument();
                xw.Close();

                // Create Zip
                string ZipFileName=Path.ChangeExtension(txtWidgetFile.Text, "wgt");

                string[] SourceFileName={txtWidgetFile.Text, txtWidgetIcon.Text, "config.xml"};

                FileStream fZip = File.Create(ZipFileName);
                ZipOutputStream zipOStream = new ZipOutputStream(fZip);

                foreach (TreeNode n in treeView1.Nodes)
                {
                    if (n.Checked)
                    {
                        if (n.Nodes.Count == 0)
                        {
                            CompressFile(zipOStream, n.Text);
                        }
                        else
                        {
                            foreach (TreeNode ChildNode in n.Nodes)
                            {
                                if (ChildNode.Checked)
                                {
                                    CompressFile(zipOStream, n.Text + @"\" + ChildNode.Text);
                                }
                            }
                        }
                    }
                }

                zipOStream.Finish();
                zipOStream.Close();
                
                MessageBox.Show(Resource1.strCreateSuccess);
            }
        }

        private void CompressFile(ZipOutputStream zipOStream, string FileName)
        {
            ZipEntry entry = new ZipEntry(FileName);
            zipOStream.PutNextEntry(entry);

            FileStream fs = File.OpenRead(FileName);
            try
            {
                byte[] transferBuffer = new byte[1024];
                int bytesRead = 0;
                do
                {
                    bytesRead = fs.Read(transferBuffer, 0, transferBuffer.Length);
                    zipOStream.Write(transferBuffer, 0, bytesRead);
                } while (bytesRead > 0);
            }
            finally
            {
                fs.Close();
                fs.Dispose();
            }
        }

        private void txtWidgetName_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtWidgetName, "");
        }

        private void txtWidgetFile_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtWidgetFile, "");
        }

        private void txtWidgetIcon_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtWidgetIcon, "");
        }

        private void txtWidgetDescription_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.SetError(lbWidgetDescription, "");
        }

        private void txtWidgetID_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtWidgetID, "");
        }

        private void txtWidgetVersion_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtWidgetVersion, "");
        }

        private void mnuChinese_Click(object sender, EventArgs e)
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-TW");
            Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-TW");
            UpdateUI();
        }

        private void mnuEnglish_Click(object sender, EventArgs e)
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            UpdateUI();
        }

    }
}
